package core.util;

/**
 * @author songduk park
 * 
 */
public class MonthOrWeek {

	int start;
	int end;
	int type;

	int size;

	public static final int month = 1;
	public static final int week = 2;

	int i_start;
	int i_end;

	private int num_months = 12;
	private int num_weeks = 53;

	public MonthOrWeek(int start, int end, int type) {
		this.start = start;
		this.end = end;
		this.type = type;

		if (type == month) {
			this.i_start = convert(start, num_months);
			this.i_end = convert(end, num_months);

		} else {
			this.i_start = convert(start, num_weeks);
			this.i_end = convert(end, num_weeks);
		}

		this.size = i_end - i_start + 1;
	}

	public void print() {

	}

	public int toDate(int num) {
		int year;
		int month_or_week;

		int denominator;
		if (type == month) {
			denominator = num_months;
		} else {
			denominator = num_weeks;
		}

		year = (num - 1) / denominator;
		month_or_week = (num - 1) % denominator + 1;

		return year * 100 + month_or_week;
	}

	public int size() {
		return this.size;
	}

	public int convert(int date, int denominator) {
		int year = (date / 100);
		int month_or_week = date % 100;

		return (year * denominator) + month_or_week % denominator;
	}

	public int getIStart() {
		return i_start;
	}

	public int getIEnd() {
		return i_end;
	}

	public static void main(String[] args) {
		MonthOrWeek date = new MonthOrWeek(201011, 201102, 1);

		for (int i = date.getIStart(); i < date.getIEnd(); i++) {
			System.out.println(i + " =" + date.toDate(i));
		}
	}
}
